﻿function gerarProjecto(nome, largura, altura, color, full, preload, deep, analytics, menu, paginas, destino)
{
	var libraryURL = fl.configURI+"FJProject";
	
	//copiar src
	var folderList = FLfile.listFolder(libraryURL+"/src", "directories");
	var fileList = FLfile.listFolder(libraryURL+"/src", "files");
	FLfile.createFolder(destino+"/src");
	copyFolders(libraryURL+"/src", destino+"/src", folderList);
	copyFiles(libraryURL+"/src", destino+"/src", fileList);
	
	//editar Model
	var modelText = FLfile.read(destino+"/src/Model.as");
	modelText = modelText.split("preload = true").join("preload = "+preload);
	modelText = modelText.split("deepLinking = false").join("deepLinking = "+deep);
	modelText = modelText.split("analytics = false").join("analytics = "+analytics);
	FLfile.write(destino+"/src/Model.as", modelText);	
	
	//copiar e editar deploy
	FLfile.createFolder(destino+"/deploy");
	FLfile.copy(libraryURL+"/deploy/expressInstall.swf", destino+"/deploy/expressInstall.swf");
	FLfile.copy(libraryURL+"/deploy/swfobject.js", destino+"/deploy/swfobject.js");
	FLfile.copy(libraryURL+"/deploy/swfaddress.js", destino+"/deploy/swfaddress.js");
	if(full){
		FLfile.copy(libraryURL+"/deploy/index-full.html", destino+"/deploy/index.html");
		FLfile.copy(libraryURL+"/deploy/swffit.js", destino+"/deploy/swffit.js");
	} else {
		FLfile.copy(libraryURL+"/deploy/index-center.html", destino+"/deploy/index.html");
	}
	var indexText = FLfile.read(destino+"/deploy/index.html");
	indexText = indexText.split("<%NOME%>").join(nome);
	indexText = indexText.split("<%LARGURA%>").join(largura);
	indexText = indexText.split("<%ALTURA%>").join(altura);
	FLfile.write(destino+"/deploy/index.html", indexText);
	
	//gerar FLA principal
	var intervalo = 10;
	var FLAURL = destino+"/"+nome+".fla";
	if( FLfile.exists(FLAURL) ){
		FLfile.remove(FLAURL);
	}
	FLfile.copy( libraryURL+"/template_FJSite.fla", FLAURL);
	fl.openDocument(FLAURL);
	fl.getDocumentDOM().width = largura;
	fl.getDocumentDOM().height = altura;
	fl.getDocumentDOM().backgroundColor = "#"+color;
	fl.getDocumentDOM().getTimeline().insertFrames(paginas.length*intervalo+Number(preload)*intervalo);
	//preload
	if(preload){
		fl.getDocumentDOM().getTimeline().layers[0].frames[0].name = "Preload";
		if( fl.getDocumentDOM().library.duplicateItem("FJProjectTemplates/template_FJPreload") ){
			fl.getDocumentDOM().library.selectItem("FJProjectTemplates/template_FJPreload copy")
			fl.getDocumentDOM().library.renameItem( "Preload" );
			fl.getDocumentDOM().library.moveToFolder("", "FJProjectTemplates/Preload");
			var nItem = fl.getDocumentDOM().library.findItemIndex("Preload");
			fl.getDocumentDOM().library.items[nItem].linkageExportForAS = true;
			fl.getDocumentDOM().library.items[nItem].linkageBaseClass = "fj.ui.FJPreload";
			fl.getDocumentDOM().getTimeline().currentLayer = 1;
			fl.getDocumentDOM().library.addItemToDocument({x:largura/2, y:altura/2}, "Preload" );
			var elem = fl.getDocumentDOM().getTimeline().layers[1].frames[0].elements[0];
			elem.name = "preload";
		}
	}
	//conteudos
	for(var i=0; i < paginas.length; i++){
		var j = Number(preload)+i;
		if(i!=0){
			fl.getDocumentDOM().getTimeline().currentLayer = 0;
			fl.getDocumentDOM().getTimeline().insertBlankKeyframe( j * intervalo );
			fl.getDocumentDOM().getTimeline().currentLayer = 1;
			fl.getDocumentDOM().getTimeline().insertBlankKeyframe( j * intervalo );
		} else {
			if(preload){
				fl.getDocumentDOM().getTimeline().currentLayer = 0;
				fl.getDocumentDOM().getTimeline().insertBlankKeyframe( j * intervalo );
				fl.getDocumentDOM().getTimeline().currentLayer = 1;
				fl.getDocumentDOM().getTimeline().insertBlankKeyframe( j * intervalo );
			} else {
				fl.getDocumentDOM().getTimeline().currentLayer = 1;
			}
		}
		fl.getDocumentDOM().getTimeline().layers[0].frames[ j * intervalo ].name = paginas[i];
		fl.getDocumentDOM().getTimeline().currentFrame = j * intervalo;
		if( fl.getDocumentDOM().library.duplicateItem("FJProjectTemplates/template_FJSection") ){
			fl.getDocumentDOM().library.selectItem("FJProjectTemplates/template_FJSection copy")
			fl.getDocumentDOM().library.renameItem( paginas[i]+"Section");
			fl.getDocumentDOM().library.moveToFolder("", "FJProjectTemplates/"+paginas[i]+"Section");
			var nItem = fl.getDocumentDOM().library.findItemIndex(paginas[i]+"Section");
			fl.getDocumentDOM().library.items[nItem].linkageExportForAS = true;
			fl.getDocumentDOM().library.items[nItem].linkageBaseClass = "fj.site.FJSection";
			fl.getDocumentDOM().library.addItemToDocument({x:0, y:0}, paginas[i]+"Section" );
		}
	}
	//menu
	if(menu){
		fl.getDocumentDOM().library.duplicateItem("FJProjectTemplates/template_FJSiteMenu");
		fl.getDocumentDOM().library.selectItem("FJProjectTemplates/template_FJSiteMenu copy");
		fl.getDocumentDOM().library.renameItem( "SiteMenu" );
		fl.getDocumentDOM().library.moveToFolder("", "FJProjectTemplates/SiteMenu");
		var nItem = fl.getDocumentDOM().library.findItemIndex("SiteMenu");
		fl.getDocumentDOM().library.items[nItem].linkageExportForAS = true;
		fl.getDocumentDOM().library.items[nItem].linkageBaseClass = "fj.site.FJSiteMenu";
		fl.getDocumentDOM().getTimeline().addNewLayer("Menu");
		if(preload){
			fl.getDocumentDOM().getTimeline().insertKeyframe(10);
			fl.getDocumentDOM().getTimeline().currentFrame = 10;
		}
		fl.getDocumentDOM().library.addItemToDocument({x:0, y:50}, "SiteMenu" );
		fl.getDocumentDOM().selection = fl.getDocumentDOM().getTimeline().layers[1].frames[ fl.getDocumentDOM().getTimeline().currentFrame ].elements;
		var elem = fl.getDocumentDOM().getTimeline().layers[1].frames[ fl.getDocumentDOM().getTimeline().currentFrame ].elements;
		elem[0].name = "menu";
		fl.getDocumentDOM().enterEditMode('inPlace');
		for(var i=0; i < paginas.length; i++){
			fl.getDocumentDOM().library.duplicateItem("FJProjectTemplates/template_FJButton");
			fl.getDocumentDOM().library.selectItem("FJProjectTemplates/template_FJButton copy");
			fl.getDocumentDOM().library.renameItem( "SiteMenu"+paginas[i] );
			fl.getDocumentDOM().library.moveToFolder("", "FJProjectTemplates/SiteMenu"+paginas[i]);
			var nItem = fl.getDocumentDOM().library.findItemIndex("SiteMenu"+paginas[i]);
			fl.getDocumentDOM().library.items[nItem].linkageExportForAS = true;
			fl.getDocumentDOM().library.items[nItem].linkageBaseClass = "fj.ui.FJButton";
			fl.getDocumentDOM().getTimeline().currentLayer = 0;
			fl.getDocumentDOM().library.addItemToDocument({x:100*i+100, y:0}, "SiteMenu"+paginas[i] );
			fl.getDocumentDOM().selection = new Array( fl.getDocumentDOM().getTimeline().layers[0].frames[0].elements[i] );
			var elem = fl.getDocumentDOM().getTimeline().layers[0].frames[0].elements;
			elem[i].name = paginas[i];
			fl.getDocumentDOM().enterEditMode('inPlace');
			fl.getDocumentDOM().addNewText({left:-50, top:-10, right:50, bottom:25} , paginas[i] ); 
			fl.getDocumentDOM().exitEditMode();
		}
		fl.getDocumentDOM().exitEditMode();
	}
	///publish
	var publishText = FLfile.read( libraryURL+"/template_publish.xml" );
	publishText = publishText.split("<%NOME%>").join(nome);
	fl.getDocumentDOM().importPublishProfileString(publishText);
	fl.getDocumentDOM().publish();
	//save and close
	fl.getDocumentDOM().save();
	fl.getDocumentDOM().close();
};
//funcoes
function copyFolders(sourcePath, targetPath, folderList)
{
	var i = folderList.length;
	while (i--)
	{
		FLfile.createFolder(targetPath + "/" + folderList[i]);
		copyFiles(sourcePath + "/" + folderList[i], targetPath + "/" + folderList[i], FLfile.listFolder(sourcePath + "/" + folderList[i], "files"));
		copyFolders(sourcePath + "/" + folderList[i], targetPath + "/" + folderList[i], FLfile.listFolder(sourcePath + "/" + folderList[i], "directories"));
	}
}
function copyFiles(sourcePath, targetPath, fileList)
{
	var i = fileList.length;
	while (i--)
	{
		FLfile.copy(sourcePath + "/" + fileList[i], targetPath + "/" + fileList[i]);
	}
}